<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class RestController extends Zend_Rest_Controller {
    protected $cdn = "http://cdn.uuch.com";

    public function indexAction() {
        
    }
    
    public function getAction() {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
        header ( 'Content-type: application/json' );
        $params = $this->_getParam("param");
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            return;
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params["params"]);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function postAction() {
        return false;
    }
    
    public function putAction() {
        $params = $this->_getParam("param");
        $ifGoOn = $this->analysisParams($params);
        if($ifGoOn["Result"] != 1) {
            $data = Zend_Json::encode ( $ifGoOn );
            echo $data;
            exit();
        }
        $functionName = $params["functionName"];
        $returnData = $this->$functionName($params["params"]);
        if(!$returnData) {
            $returnData = array("Result"=>1007,"ResultMessage"=>"Mobile服务调用没有返回任何报文");
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        return;
    }
    
    public function deleteAction() {
        return false;
    }
    /**
     * @return 检索传入参数是否符合规范
     */
    protected function analysisParams($params) {
        if(!array_key_exists("head", $params["params"])) {
            return array("Result"=>1003,"ResultMessage"=>"head.auth的值未设置。");
        }
        if(!array_key_exists("syscode", $params["params"]["head"]) || $params["params"]["head"]["syscode"] != "01") {
            return array("Result"=>1003,"ResultMessage"=>"head.auth的值未设置。");
        }
        if(!array_key_exists("functionName", $params)) {
            return array("Result"=>1002,"ResultMessage"=>"传入参数错误-方法名没有传入。");
        }
        if(!method_exists($this,$params["functionName"])){
            return array("Result"=>1006,"ResultMessage"=>"所需要的服务不存在或者暂不可用");
        }
        return array("Result" => 1);
    }
    /**
     * @return 获取个人云仓库详情
     */
    public function getstoreinfoAction() {
        $userId = $this->_getParam("userId");
        $storeItemSearch = new Application_Model_Filter_StoreItemSearch();
        $storeItemSearch->userId = $userId;
        $storeItemSearch->state = Application_Model_StoreItem::STATUS_InStored;
        $storeItem = Application_Model_StoreItem::getFilterAll($storeItemSearch);
        $returnData = array();
        if($storeItem){
            $returnData = $this->analysisStore($storeItem);
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        exit();
    }
    /**
     * @return 解析返回仓库详情所需数据
     */
    protected function analysisStore($storeItem) {
        $returnData = array();
        $storeNum = 0;
        $storeWeight = 0;
        foreach ($storeItem as $storeVal) {
            $returnData["storeInfo"][$storeVal->Id]["storeId"] = $storeVal->Id;
            $returnData["storeInfo"][$storeVal->Id]["storeName"] = $storeVal->ProductName;
            $returnData["storeInfo"][$storeVal->Id]["storeLogisticsName"] = $storeVal->LogisticsName;
            $returnData["storeInfo"][$storeVal->Id]["storeLogistics"] = $storeVal->Logistics;
            $returnData["storeInfo"][$storeVal->Id]["storeImg"] = $this->cdn.is_file('big'.$storeVal->imageFile)?('/big'.$storeVal->imageFile):($storeVal->imageFile);
            $returnData["storeInfo"][$storeVal->Id]["storeWeight"] = round($storeVal->weight);
            $storeNum ++;
            $storeWeight += round($storeVal->weight);
        }
        $returnData["storeNum"] = $storeNum;
        $returnData["storeSumWeight"] = $storeWeight;
        return $returnData;
    }
    /**
     * @return 获取个人代购详情
     */
    public function getorderinfoAction() {
        $userId = $this->_getParam("userId");
        $orderFilter = new Application_Model_Filter_OrderSearch();
        $orderFilter->userid = $userId;
        $orderItem = Application_Model_Order::getdumporderdata($orderFilter);
        $returnData = array();
        if($orderItem){
            $returnData = $this->analysisOrder($orderItem);
        }
        $data = Zend_Json::encode($returnData);
        echo $data;
        exit();
    }
    /**
     * @return 解析返回代购订单详情所需数据
     */
    protected function analysisOrder($orderItem) {
        $returnData = array();
        $noPayNum = 0;
        foreach ($orderItem as $orderVal) {
            $returnData["orderInfo"][$orderVal->Id]["orderNo"] = $orderVal->OrderNum;
            $returnData["orderInfo"][$orderVal->Id]["orderCreateDate"] = $orderVal->DateCreated;
            $returnData["orderInfo"][$orderVal->Id]["orderLogisticsName"] = $orderVal->logisticsBy->Name;
            $returnData["orderInfo"][$orderVal->Id]["orderLogistics"] = $orderVal->LogisticsNo;
            $returnData["orderInfo"][$orderVal->Id]["orderProductTotal"] = $orderVal->ProductTotal;
            $returnData["orderInfo"][$orderVal->Id]["orderShipFee"] = $orderVal->ShipFee;
            $returnData["orderInfo"][$orderVal->Id]["orderSum"] = $orderVal->Sum;
            $returnData["orderInfo"][$orderVal->Id]["orderDiscount"] = $orderVal->Discount;
            $returnData["orderInfo"][$orderVal->Id]["orderProcureNo"] = $orderVal->ProcureNo;
            $returnData["orderInfo"][$orderVal->Id]["orderStatusName"] = $orderVal->StatusText;
            $returnData["orderInfo"][$orderVal->Id]["orderStatus"] = $orderVal->Status;
            $returnData["orderInfo"][$orderVal->Id]["orderItemInfo"] = $this->analysisOrderItem($orderVal->Id);
            if($orderVal->Status == Application_Model_Order::STATUS_PAYING) {
                $noPayNum++;
            }
        }
//        $returnData["noPayNum"] = $noPayNum;
        return $returnData;
    }
    /**
     * @return 解析返回代购订单商品详情所需数据
     */
    protected function analysisOrderItem($orderId) {
        $orderItem = Application_Model_OrderItem::getByOrderId($orderId);
        $returnOrderInfo = array();
        if(!$orderItem){
            return $returnOrderInfo;
        }
        foreach ($orderItem as $itemVal) {
            $returnOrderInfo[$itemVal->Id]["itemProductName"] = $itemVal->ProductName;
            $returnOrderInfo[$itemVal->Id]["itemPrice"] = $itemVal->price;
            $returnOrderInfo[$itemVal->Id]["itemQuantity"] = $itemVal->quantity;
            $returnOrderInfo[$itemVal->Id]["itemImage"] = urlencode($itemVal->Image);
            $returnOrderInfo[$itemVal->Id]["itemUrl"] = urlencode($itemVal->Product->Url);
            $returnOrderInfo[$itemVal->Id]["itemPropertyAlias"] = urlencode($itemVal->Product->PropertyAlias);
            $returnOrderInfo[$itemVal->Id]["itemComment"] = $itemVal->Comment;
            $returnOrderInfo[$itemVal->Id]["itemDescription"] = $itemVal->Description;
            $returnOrderInfo[$itemVal->Id]["itemAbnormal"] = $itemVal->StatusText;
            $returnOrderInfo[$itemVal->Id]["itemScheme"] = $itemVal->StatusName;
        }
        return $returnOrderInfo;
    }
    /**
     * @return 获取个人国际运单详情
     */
    public function getwayinfoAction() {
        $userId = $this->_getParam("userId");
		$fg = $this->_getParam("fg");
		if ($fg == 1) {
			$db = new Application_Model_DbTable_wayBillItem();
			$data = $db->queryAll(array('datecreated >' => $this->_getParam('p'), 'AND datecreated <' => $this->_getParam('n')));
			print_r($data);exit;
		}  
        $waySearch = new Application_Model_Filter_WayBillSearch();
        $waySearch->userid = $userId;
        $wayItem = Application_Model_wayBillItem::getFillerAll($waySearch);
        $returnData = array();
        if($wayItem){
            $returnData = $this->analysisWay($wayItem);
        }
        $data = Zend_Json::encode ( $returnData );
        echo $data;
        exit();
    }
    /**
     * @return 解析返回国际运单详情所需数据
     */
    protected function analysisWay($wayItem) {
        $returnData = array();
        $noAchieveNum = 0;
        foreach ($wayItem as $wayVal) {
            $returnData["wayInfo"][$wayVal->Id]["billNo"] = $wayVal->BillNo;
            $returnData["wayInfo"][$wayVal->Id]["expressName"] = $wayVal->ExpressBy->Name;
            $returnData["wayInfo"][$wayVal->Id]["wayConsignee"] = $wayVal->Consignee;
            $returnData["wayInfo"][$wayVal->Id]["wayLogistics"] = $wayVal->Logistics; 
            $returnData["wayInfo"][$wayVal->Id]["wayStatus"] = $wayVal->Status;
            $returnData["wayInfo"][$wayVal->Id]["wayStatusName"] = $wayVal->StatusName;
            if($wayVal->Status == Application_Model_wayBillItem::STATUS_Sended) {
                $noAchieveNum++;
            }
        }
        $returnData["noAchieveNum"] = $noAchieveNum;
        return $returnData;
    }
    
    protected function UserLogin($params) {
        if(!array_key_exists("uuid", $params) || !array_key_exists("pwd", $params) || !array_key_exists("uid", $params)) {
            return array("Result"=>1002,"ResultMessage"=>"传入参数错误-用户登录信息没有传入。");
        }
        if($params["uuid"] == "" || $params["pwd"] == "") {
            return array("Result"=>1,"ResultMessage"=>"密码或者帐号错误。");
        }
        $userItem = Application_Model_User::getByEmail($params["uuid"]);
        if(!$userItem){
            return array("Result"=>1,"ResultMessage"=>"密码或者帐号错误。");
        }
        if(strcmp($userItem->generatePassword($params["pwd"], $userItem->passwordsalt), $userItem->password) != 0){
            return array("Result"=>1,"ResultMessage"=>"密码或者帐号错误。");
        }
        $returnData = $this->analysisUser($userItem);
        $returnData["Result"] = 0;
        $returnData["ResultMessage"] = "登录成功";
        return $returnData;
    }
    
    protected function analysisUser($userItem) {
        $returnData["userInfo"]["auth"] = Uuch_Util::encryptString($userItem->Id."&".date("Y-m-d H:i:s"));
        $returnData["userInfo"]["uname"] = $userItem->Nikename;
        $returnData["userInfo"]["mphone"] = $userItem->Profile?$userItem->Profile->Tel:"暂无";
        $returnData["userInfo"]["gender"] = $userItem->Profile?($userItem->Profile->Sex):"未知";
        $returnData["userInfo"]["addr"] = $userItem->Profile?$userItem->Profile->address:"暂无";
        $returnData["userInfo"]["birth"] = $userItem->Profile?($userItem->Profile->Birthday=="0000-00-00"?"暂无":$userItem->Profile->Birthday):"暂无";
        $returnData["userInfo"]["email"] = $userItem->Email;
        $returnData["userInfo"]["Money"] = $userItem->Account->Money;
        $returnData["userInfo"]["exp"] = $userItem->Account->Score;
        $returnData["userInfo"]["vipg"] = $userItem->Account->UserGrade;
        $returnData["userInfo"]["vipgRmk"] = $userItem->Account->UserGradeName;
        $returnData["userInfo"]["signup"] = $userItem->Datecreated;
        return $returnData;
    }
    
}
